package davidlauzon.activerecord.recordset;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import davidlauzon.activerecord.ActiveRecord;
import davidlauzon.activerecord.nodes.field.Field;
import davidlauzon.activerecord.nodes.field.FloatField;
import davidlauzon.activerecord.nodes.field.IntegerField;
import davidlauzon.activerecord.nodes.field.StringField;


public abstract class RecordSet
{
    protected Class<ActiveRecord> _recordClass;
    protected List<Field>         _fields;

    public abstract boolean next() throws SQLException;
    public abstract boolean hasNext() throws SQLException;
    public abstract void    close();
    public abstract int     getSize();
    public abstract int     getPosition();
    
    public abstract boolean getBoolean(int index)  throws SQLException;
    public abstract float   getFloat(int index)  throws SQLException;
    public abstract double  getDouble(int index)  throws SQLException;
    public abstract int     getInt(int index)  throws SQLException;
    public abstract long    getLong(int index)  throws SQLException;
    public abstract String  getString(int index)  throws SQLException;
    public abstract Date    getDate(int index)  throws SQLException;
    public abstract boolean lastColumnReadWasNull()  throws SQLException;

    public void setRecordClass(Class<ActiveRecord> recordClass) { _recordClass = recordClass; }
    
    public List<Field> getFieldList() { return _fields; }
    
    public void setFieldList(List<Field> fieldList) {
        _fields = fieldList;
    }

    public ActiveRecord fetchRecord() throws SQLException {
        HashMap<Field, Object> attributes = new HashMap<Field, Object>();
        Object value;
        int counter = 0;
        
        for (Field field : getFieldList())
        {
            if (field instanceof StringField)
                value = getString(counter);
            else if (field instanceof IntegerField)
                value = getLong(counter);
            else if (field instanceof FloatField)
                value = getFloat(counter);
            else
                value = getString(counter);
            
            attributes.put(field, value);
            counter++;
        }
        
        ActiveRecord record = ActiveRecord.build( _recordClass, attributes );
        
        return record;
    }

//    public long getCount() throws SQLException {
//        next();
//        return getLong(0);
//    }
}
